package tools.settings.gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.*;
import tools.settings.SettingsAdapter;

/**
 *
 * @author Lynne Vandousselaere
 */
public class SimpleSettingsGUI extends JPanel implements ActionListener {

    // Array van de knoppen en bijhorende afbeeldingen
    private JFrame window;
    private SettingsAdapter sa = SettingsAdapter.getInstance();

    public SimpleSettingsGUI(JFrame jframe) {
        this.window = jframe;
        init();
    }

    private void init() {
        removeAll();
        setLayout(new GridLayout(0, 1));
        //setPreferredSize(new Dimension(200, 200));

        JTitlePanel subpanel;
        JLabel label;
        JButton button;

        /**
         * XML File
         */
        subpanel = new JTitlePanel("Check XML File");

        button = new JButton("Check XML file ...");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JSettingsPane.checkXMLFile(window);
            }
        });
        subpanel.add(button);

        add(subpanel);

        /**
         * XML File
         */
        subpanel = new JTitlePanel("Add settings files (XML format)");

        button = new JButton("Add settings file ...");
        button.addActionListener(this);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JSettingsPane.addXMLFiles(window);
            }
        });
        subpanel.add(button);
        subpanel.add(getSettingFiles());

        add(subpanel);


        /**
         * Default folder
         */
        subpanel = new JTitlePanel("Default folder");
        label = new JLabel("Current default folder = ");
        subpanel.add(label);
        label = new JLabel("      " + sa.getDefault_folder().getPath());
        subpanel.add(label);
        label = new JLabel(" ");
        subpanel.add(label);

        button = new JButton("Set default folder ...");
        button.addActionListener(this);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JSettingsPane.setDefaultFolderDialog(window);
            }
        });
        subpanel.add(button);

        add(subpanel);

        /**
         * Constants
         */
        subpanel = new JTitlePanel("GUI settings");

        label = new JLabel("Information messages = " + (JSettingsPane.information_msg_on ? "ON" : "OFF"));
        subpanel.add(label);

        button = new JButton("Change");
        button.addActionListener(this);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JSettingsPane.information_msg_on = !JSettingsPane.information_msg_on;
            }
        });
        subpanel.add(button);

        label = new JLabel(" ");
        subpanel.add(label);

        label = new JLabel("Verify XML files on add = " + (JSettingsPane.verifyFilesOnAdd ? "ON" : "OFF"));
        subpanel.add(label);

        button = new JButton("Change");
        button.addActionListener(this);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JSettingsPane.verifyFilesOnAdd = !JSettingsPane.verifyFilesOnAdd;
            }
        });
        subpanel.add(button);

        add(subpanel);

        /**
         * Set up window
         */
        window.setContentPane(this);
        window.pack();
        window.setVisible(true);
    }

    /**
     * Uitvoerprogramma 
     */
    public static void main(String[] args) {
        JFrame window = new JFrame("SimpleSettingsGUI (c) 2009 LVD");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        new SimpleSettingsGUI(window);
    }

    public void actionPerformed(ActionEvent e) {
        init();
        repaint();
    }

    private JPanel getSettingFiles() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(0,1));
        JLabel lab = new JLabel("CURRENT SETTING FILES");
        p.add(lab);
        Collection<File> fcoll = sa.getTechnologyFiles();

        if (fcoll == null)
            p.add(new JLabel("< none >"));
        else
            for (Iterator<File> it = fcoll.iterator(); it.hasNext();)
                p.add(new JLabel(it.next().getPath()));

        return p;
    }
}

/**
 * Creates a nice little border with title.
 * @author Lynne
 */
class JTitlePanel extends JPanel {

    final static boolean MULTICOLORED = false;
    final static int MAX = 10000;

    JTitlePanel(String myTitle) {
        if (MULTICOLORED) {
            setOpaque(true);
            setBackground(new Color(0, 255, 255));
        }
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder(myTitle),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    }

    //Don't allow this panel to get taller than its preferred size.
    //BoxLayout pays attention to maximum size, though most layout
    //managers don't.
}

